package com.gjs.management.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gjs.common.exception.BizException;
import com.gjs.common.manager.response.RspBody;
import com.gjs.common.pojo.dto.request.PageQuery;
import com.gjs.common.pojo.dto.response.ListPageRsp;
import com.gjs.management.pojo.dto.role.SysRoleQueryDTO;
import com.gjs.management.pojo.entity.SysPermission;
import com.gjs.management.pojo.entity.SysRole;
import com.gjs.management.pojo.vo.role.SysRoleReqVO;
import com.gjs.management.pojo.vo.role.SysRoleRspVO;
import com.gjs.management.service.ISysRolePermissionService;
import com.gjs.management.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@Slf4j
@Api(tags = "角色接口")
@RequiredArgsConstructor
@RequestMapping("/role")
public class RoleController {

    private final ISysRoleService sysRoleService;
    private final ISysRolePermissionService sysRolePermissionService;

    @GetMapping("/list")
    @ApiOperation("角色列表")
    public RspBody<ListPageRsp<SysRoleRspVO>> list(SysRoleQueryDTO queryDTO, PageQuery pageQuery){
        IPage<SysRole> sysRoleIPage = sysRoleService.page(pageQuery.buildPage(),sysRoleService.buildWrapper(queryDTO));
        return RspBody.success(ListPageRsp.of(sysRoleIPage, SysRoleRspVO.class));
    }

    @GetMapping("/detail")
    @ApiOperation("角色详情")
    public RspBody<SysRoleRspVO> detail(Long id){
        SysRole sysRole = sysRoleService.getById(id);
        if(sysRole == null){
            throw new BizException("找不到该角色");
        }
        SysRoleRspVO sysRoleRspVO = BeanUtil.copyProperties(sysRole, SysRoleRspVO.class);

        // 获取角色权限
        List<SysPermission> sysPermissionList = sysRolePermissionService.getPermissionListByRoleId(sysRole.getId());
        if(sysPermissionList != null && !sysPermissionList.isEmpty()){
            Set<String> sysPermissionRspVOSet = sysPermissionList
                    .stream()
                    .map(SysPermission::getPermissionKey)
                    .collect(Collectors.toSet());
            // 设置角色权限
            sysRoleRspVO.setPermissionKeySet(sysPermissionRspVOSet);
        }

        return RspBody.success(sysRoleRspVO);
    }

    @PostMapping("/add")
    @ApiOperation("添加")
    @Transactional
    public RspBody<SysRoleRspVO> add(@RequestBody SysRoleReqVO sysRoleReqVo){
        SysRole sysRole = BeanUtil.copyProperties(sysRoleReqVo,SysRole.class);
        try{
            sysRoleService.save(sysRole);
            if(!sysRoleReqVo.getPermissionKeySet().isEmpty()){
                sysRolePermissionService.saveByPermissionKeySet(sysRole,sysRoleReqVo.getPermissionKeySet());
            }
        }catch (Exception e){
            throw new BizException("添加角色失败：" + e.getMessage());
        }

        return RspBody.success(BeanUtil.copyProperties(sysRole, SysRoleRspVO.class));
    }

    @PostMapping("/update")
    @ApiOperation("更新")
    @Transactional
    public RspBody<SysRoleRspVO> update(@RequestBody SysRoleReqVO sysRoleReqVo){
        SysRole sysRole = sysRoleService.getById(sysRoleReqVo.getId());
        if(sysRole == null){
            throw new BizException("找不到该角色");
        }

        try{
            BeanUtil.copyProperties(sysRoleReqVo,sysRole);
            sysRoleService.updateById(sysRole);
            sysRolePermissionService.saveByPermissionKeySet(sysRole,sysRoleReqVo.getPermissionKeySet());
        }catch (Exception e){
            throw new BizException("更新角色失败：" + e.getMessage());
        }

        return RspBody.success(BeanUtil.copyProperties(sysRole, SysRoleRspVO.class));
    }
}
